package com.dx.wx.controller;

import com.alibaba.fastjson.JSON;
import com.dx.wx.service.DxShopService;
import com.dx.wx.service.DxUserService;
import com.dx.wx.util.DxUtil;
import com.ejlchina.data.Array;
import com.ejlchina.data.Mapper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/dx/user")
public class ApiDxUserController {

    @Autowired
    private DxUserService dxUserService;

    @Autowired
    private DxShopService dxShopService;

    @GetMapping("/info")
    public R info(@LoginUserId Long phone, String otherPhone, String appId) {
        String sePhone = String.valueOf(phone);
        if (otherPhone != null && !otherPhone.equals("")) {
            sePhone = otherPhone;
        }
        Mapper userMapper = dxUserService.findByMobile(sePhone, appId);
        Integer userCount = dxUserService.findByTop(phone).size();
        Integer shopCount = dxShopService.findShop(phone).size();//主管的门店数量
        Integer manShopCount = dxShopService.findShopByTop(phone).size();//主管的连锁数量
        return R.ok(userMapper.toString()).putExtra("userCount", userCount).putExtra("shopCount", shopCount).putExtra("manShopCount", manShopCount);
    }


    @IgnoreAuth
    @PostMapping("/")
    public R save(@RequestBody Map userData, String appId) {
        appId = DxUtil.getAppId(appId);
        dxUserService.saveUser(userData, appId);
        return R.ok();
    }

    @PostMapping("/shop/update")
    public R updateShop(String shopId, String[] userIds) {
        dxUserService.updateUserShop(shopId, userIds);
        return R.ok();
    }


    @PostMapping("/update")
    public R update(@LoginUserId Long phone, @RequestBody Map userData) {
//        Mapper userMapper = dxUserService.findByMobile(String.valueOf(phone));
        Map data = new HashMap();
        data.put("_id", userData.get("_id"));
        Mapper mapper = dxUserService.findById((String) userData.get("_id"));
        if(!mapper.getString("field_z79am__c").equals(phone)){ //不是自己在修改
            throw new RRException("禁止修改!");
        }
        data.put("field_W1MfE__c", userData.get("field_W1MfE__c"));//姓名
        data.put("field_Jqj1s__c", userData.get("field_Jqj1s__c"));//性别
        data.put("field_7Q38t__c", userData.get("field_7Q38t__c"));//药店
        data.put("address__c", userData.get("address__c"));//地址
//        data.put("field_Jqj1s__c", userData.get("field_z79am__c"));//手机号 暂时不修改
        dxUserService.updateUser(data);
        return R.ok();
    }

    @GetMapping("/phone")
    public R get(String phone) {
        Mapper mapper = dxUserService.findByMobile(phone);
        return R.ok(mapper.toString());
    }

    @PostMapping("/check")
    public R check(String phone, Integer type) {
        dxUserService.check(phone, type);
        return R.ok();
    }


    /**
     * 下面所有的用户
     *
     * @param phone
     * @return
     */
    @GetMapping("/my/shop/user")
    public R findMyShopUser(@LoginUserId Long phone, Long startDate, Long endDate) {
        Array array = dxUserService.findMyShopUser(phone, startDate, endDate);
        return R.ok(array.toString());
    }

    //通过上级查找
    @GetMapping("/top")
    public R findByTop(@LoginUserId Long phone, String appId) {
        Array array = dxUserService.findByTop(phone, appId);
        return R.ok(array.toString());
    }

    @GetMapping("/top/{phone}")
    public R findByTopPhone(@PathVariable Long phone, String appId) {
        Array array = dxUserService.findByTop(phone, appId);
        return R.ok(array.toString());
    }

    /**
     * 下面所有的用户
     *
     * @param phone
     * @return
     */
    @GetMapping("/man")
    public R findByMan(@LoginUserId Long phone) {
        Array array = dxUserService.findByMan(phone);
        return R.ok(array.toString());
    }


    /**
     * 查看当前人的可售范围
     * 有哪些产品
     *
     * @param phone
     * @return
     */
    @GetMapping("/goods")
    public R save(@LoginUserId Long phone) {
        Array array = dxUserService.findGoodsByUser(phone);
        return R.ok(array.toString());
    }

    /**
     * 同药店下的其他店员
     *
     * @param phone
     * @return
     */
    @GetMapping("/shop")
    public R shop(@LoginUserId Long phone, String id) {
        Array array = dxUserService.findByShop(phone, id);
        return R.ok(array.toString());
    }


    /**
     * 用户的价目表信息
     *
     * @param phone
     * @return
     */
    @GetMapping("/price")
    public R shop(@LoginUserId Long phone) {
        Array array = dxShopService.findPrice(phone);
        return R.ok(array.toString());
    }


}
